package com.czx.drivingtest.controller;

import com.czx.drivingtest.model.ResponseData;
import com.czx.drivingtest.model.entity.Statistics;
import com.czx.drivingtest.model.entity.UserArchive;
import com.czx.drivingtest.model.entity.UserQuestion;
import com.czx.drivingtest.service.UserQuestionService;
import com.czx.drivingtest.utils.JwtUtils;
import com.czx.drivingtest.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户与题目关联的控制器
 * @author czx
 * @version 2021/1/29 9:48 下午
 */
@RestController
@RequestMapping("/user_question")
public class UserQuestionController {

    @Autowired
    private UserQuestionService userQuestionService;

    /**
     * 获取用户存档
     * @return
     */
    @GetMapping(value = "/userArchive")
    public ResponseData<UserArchive> getUserArchive(Integer subject) {
        String authorization = ServletUtils.getRequestHeader("Authorization");
        long userId = JwtUtils.analysisUserId(authorization);
        UserArchive userArchive = userQuestionService.getUserArchive(userId, subject);
        return new ResponseData<>(0, "success", userArchive);
    }

    /**
     * 用户答题
     * @param status 1答错  2答对
     * @param questionId
     * @return
     */
    @PutMapping("/complete_question")
    public ResponseData<Void> completeQuestion(Integer status, Integer questionId, boolean wasInOrder) {
        String authorization = ServletUtils.getRequestHeader("Authorization");
        long userId = JwtUtils.analysisUserId(authorization);
        userQuestionService.completeQuestion(userId, status, questionId, wasInOrder);
        return new ResponseData<>(0, "success");
    }

    /**
     * 获取用户与题目的关系
     * @return
     */
    @GetMapping("/list")
    public ResponseData<List<UserQuestion>> listUserQuestions(Integer subject) {
        String authorization = ServletUtils.getRequestHeader("Authorization");
        long userId = JwtUtils.analysisUserId(authorization);
        List<UserQuestion> userQuestions = userQuestionService.listUserQuestions(userId, subject);
        return new ResponseData<>(0, "success", userQuestions);
    }

    /**
     * 获取用户的错题
     * @return
     */
    @GetMapping("/wrongs")
    public ResponseData<List<UserQuestion>> getWrongQuestions(Integer subject) {
        String authorization = ServletUtils.getRequestHeader("Authorization");
        long userId = JwtUtils.analysisUserId(authorization);
        List<UserQuestion> userQuestions = userQuestionService.getWrongQuestions(userId, subject);
        return new ResponseData<>(0, "success", userQuestions);
    }

    /**
     * 交卷接口
     * @param trueCount
     * @return
     */
    @PostMapping("/handPaper")
    public ResponseData<Void> handPaper(int trueCount, int subject) {
        userQuestionService.handPaper(trueCount, subject);
        return new ResponseData<>(0, "success");
    }

    /**
     * 获取用户的考试统计
     * @param subject
     * @return
     */
    @GetMapping("statistics")
    public ResponseData<Statistics> getStatistics(int subject) {
        String authorization = ServletUtils.getRequestHeader("Authorization");
        long userId = JwtUtils.analysisUserId(authorization);
        Statistics statistics = userQuestionService.getStatistics(userId, subject);
        return new ResponseData<>(0, "success", statistics);
    }
}
